package com.sysolve.betterCity.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sysolve.betterCity.common.Help;
import com.sysolve.betterCity.model.Activity;
import com.sysolve.betterCity.model.ActivityType;
import com.sysolve.betterCity.model.Person;
import com.sysolve.betterCity.model.Place;
import com.sysolve.betterCity.service.BetterCityService;

/**
 * Servlet implementation class for Servlet: SubmitActivity
 *
 */
 public class SubmitActivity extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public SubmitActivity() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		String domain = request.getParameter("domain");
		String userId = request.getParameter("userId");
		String username = request.getParameter("username");
		BetterCityService service = BetterCityService.getInstance();
		
		int activityType = Help.getInt(request.getParameter("activityType"));
		String activityTitle = request.getParameter("title");
		int lat = Help.getInt(request.getParameter("lat"));
		int lng = Help.getInt(request.getParameter("lng"));
		String placeTitle = request.getParameter("place");
		int isPlacePublic = Help.getInt(request.getParameter("isPlacePublic"));
		int placeId = Help.getInt(request.getParameter("placeId"));
		Date eventTime = Help.getDate(request.getParameter("eventTime"));
		
		if (activityType==0) activityType=1;
		
		String returnUrl = request.getParameter("returnUrl");
		
		Place place = new Place();
		place.setPlace(placeTitle);
		place.setLat(lat);
		place.setLng(lng);
		place.setIsPublic(isPlacePublic);
		place.setId(placeId);
		
		Person person = service.createPerson(domain, userId, username);
		Activity activity = new Activity();
		activity.setActivityType(new ActivityType(activityType));
		activity.setPlace(place);
		activity.setOwner(person);
		activity.setUpdated(new Date());
		activity.setEventTime(eventTime);
		activity.setTitle(activityTitle);
		
		service.createActivity(activity);
		
		response.setContentType("text/html");  
		response.setCharacterEncoding("utf-8");
		
        PrintWriter out = response.getWriter();
        
        out.println("<script language=\"javascript\">");
        out.println("alert('活动保存成功');");
        out.println("location.href='"+ returnUrl +"';");
        out.println("</script>");
        
        out.close();
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}   	  	    
}